<script setup name="Dept">
	import {ref, reactive} from 'vue'
	import {deptApi} from '@/api/dept.js'
	import {
		Delete,
		Edit,
		Message, Plus,
		Search,
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	//表格数据
	let deptList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const deptQuery = reactive({
		page: 1,
		limit: 10,
		name: ''
	})
	
	const loadData = () => {
		deptApi.list(deptQuery).then(result => {
			deptList.value = result.data.list
			total.value = result.data.total
			
		})
	}
	
	loadData()
	
	const onSearch = () => {
		deptQuery.page = 1
		loadData()
	}
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				deptApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
				
			})
	}
	
	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		ids.value = rows.map(row => row.id)
		console.log(ids.value);
	}
	const deleteAll = () => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				deptApi.deleteAll(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//添加、编辑
	const dialogVisible = ref(false)
	const dept = ref({})
	const title = ref('')
	const showAddDialog = () => {
		dialogVisible.value = true  //弹层出现
		title.value = '添加'
		dept.value = {}
	}
	
	const showUpdateDialog = (id) => {
		dialogVisible.value = true
		title.value = '编辑'
		dept.value = {}
		deptApi.selectById(id).then(result => {
			dept.value = result.data
		})
	}
	
	const addOrUpdate = () => {
		if (dept.value.id) {  //有值，编辑
			deptApi.update(dept.value).then(result => {
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value = false  //弹层消失
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		} else {   //无值，添加
			deptApi.add(dept.value).then(result => {
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value = false
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		}
	}
	
	// 重置搜索
	const originData = () => ({name: ""});
	const resetForm = () => {
		Object.assign(deptQuery, originData());
		loadData()
	};
	
	//状态更改
	const handleSwitchChange = (row) => {
		deptApi.updateStatus(row.id, row.status).then(result => {
			if (result.code === 0) {
				ElMessage.success(result.msg)
			} else {
				ElMessage.error(result.msg)
			}
		})
	}
</script>

<template>
	<div class="back">
		<el-card class="">
			<template #header>
				<div class="header">
					<el-button type="success" :icon="Plus" @click="showAddDialog">添加</el-button>
					<el-button type="danger" :icon="Delete" @click="deleteAll">批量删除</el-button>
				</div>
			</template>
			<!--		搜索框-->
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item label="部门名称">
					<el-input v-model="deptQuery.name" placeholder="请输入部门名称" clearable/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="deptList" border stripe style="width: 100%;font-size:16px"
					  @selection-change="handleSelectionChange" height="450px" >
				<el-table-column type="selection" :selectable="selectable" min-width="50" align="center"/>
				<el-table-column fixed prop="id" label="ID" sortable min-width="70" align="center"/>
				<el-table-column prop="name" label="部门名称" align="center" min-min-width="90"/>
				<el-table-column prop="content" label="部门描述" align="center" min-width="200" show-overflow-tooltip/>
				<el-table-column prop="status" label="状态" align="center" min-width="80">
					<template #default="scope">
						<el-switch
							v-model="scope.row.status"
							class="ml-2"
							inline-prompt
							size="large"
							style="--el-switch-on-color: #409EFF; --el-switch-off-color: #909399"
							active-text="运行"
							:active-value="1"
							inactive-text="撤销"
							:inactive-value="0"
							@change="handleSwitchChange(scope.row)"
						/>
					</template>
				</el-table-column>
				<el-table-column prop="updateTime" label="更新时间" align="center" min-width="180"/>
				<el-table-column fixed="right" label="操作" min-width="270" align="center">
					<template #default="{row}">
						<el-button type="primary" :icon="Edit" round @click="showUpdateDialog(row.id)">编辑</el-button>
						<el-button type="danger" :icon="Delete" round @click="deleteById(row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="deptQuery.page"
				v-model:page-size="deptQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
		
		<!--		添加编辑弹出框-->
		<el-dialog v-model="dialogVisible" :title="title" width="500" :lock-scroll="false">
			<el-form :model="dept">
				<el-form-item label="部门名称：" >
					<el-input v-model="dept.name" autocomplete="off"/>
				</el-form-item>
				<el-form-item label="部门描述：">
					<el-input v-model="dept.content" type="textarea" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="addOrUpdate">
						确认
					</el-button>
				</div>
			</template>
		</el-dialog>
	</div>

</template>

<style scoped>

</style>